from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from .models import UserKu
from django.urls import reverse
from .baidu_api import *
from .import chinese_pinyin

# Create your views here.

def register(request):

    context = {}
    return render(request,'login/register.html',context)


def register_yunsuan(request):
    
    info = request.POST
    info_name = info['userinfo']
    info_grade =info['usergrade']
    info_id = info['username']
    #print(type(info_id))
    pswd = info['password']
    #print(info)
    group = info['usergrade']
    info_user_id = chinese_pinyin.szm_convert(str_input = info_name)
    img64 = info['img64'].replace('data:image/jpeg;base64,','')
    #print(img64)
    #info_obj = UserKu.objects.get(user_id = info_id)
    #print(type(info_obj))
    
    if UserKu.objects.filter(user_id = info_id).exists():
        context = {'message':'你注册的帐号已存在请重新输入！'}
        return render(request,'login/register.html',context)
    else:
        try:
            
            
            
            baidu_search = SearchFace(img_64 = img64).search(group_id_list = group)
            #print(baidu_search)
            info_user_name = baidu_search['result']['user_list'][0]['user_info']
            info_user_id = baidu_search['result']['user_list'][0]['user_id']
            info_score = baidu_search['result']['user_list'][0]['score']
            

            
            info_obj = UserKu.objects.create(user_name = info_name,
                                             user_id = info_id,
                                             user_pwd = pswd,
                                             user_img64 = img64)
            
        except ConnectionError:
            context = {'message':'请检查您的网络是否连接！'}
            return render(request,'login/register.html',context)
        else:
            if info_score < 80:
                baidu_add = AddFace(img_64 = img64).add_face_to_ku(info_user_id,
                                                                info_name,
                                                                group_id = info_grade)
                #print(baidu_add)
                if baidu_add['error_msg'] == 'SUCCESS':
                    context = {'message':'恭喜你注册成功！','message_n':'恭喜'}
                    return render(request,'login/register.html',context)
                else:
                    context = {'message':'请您确保拍摄时脸部清晰！'}
                    return render(request,'login/register.html',context)
            else:
                context = {'message':'您已有账号请勿重复注册！'}
                return render(request,'login/register.html',context)



def login(request):
    
    context = {}
    return render(request,'login/login.html',context)


def login_yunsuan(request):
    
    info = request.POST
    #print(info)
    #info_name = info['userinfo']
    info_id = info['username']  
    pswd = info['password']
    #img64 = info['img64']
    
    
    try:
                
        info_obj = UserKu.objects.get(user_id = info_id)
                
    except UserKu.DoesNotExist:
        context = {'message':'你输入的帐号或密码不正确'}
        return render(request,'login/login.html',context)
    else:
        if info_obj.user_pwd == pswd:
            return HttpResponseRedirect(reverse('novel:search_page'))
        else:
            context = {'message':'你输入的帐号或密码不正确'}
            return render(request,'login/login.html',context)

     

def face_login(request):
    info = request.POST
    #print(info)
    img64 = info['img64'].replace('data:image/jpeg;base64,','')
    group = info['usergrade']
    #print(group)
    #face_token = '8e09c7e9c322046c1437a6e950163912'
    baidu_search = SearchFace(img_64 = img64).search(group_id_list = group)
    #print(baidu_search)
    info_user_name = baidu_search['result']['user_list'][0]['user_info']
    info_user_id = baidu_search['result']['user_list'][0]['user_id']
    info_score = baidu_search['result']['user_list'][0]['score']
    if info_score >= 80:
        return HttpResponseRedirect(reverse('novel:search_page'))
    else:
        context = {'message':'你还没注册，请注册后登陆！'}
        return render(request,'login/login.html',context)
        





def ch_pwd(request):
    
    context = {}
    return render(request,'login/login.html',context)


def ch_pwd_yunsuan(request):
    
    context = {}
    return render(request,'login/login.html',context)

















